// ==UserScript==
// @name wikignpi-preview2Left
// @author      Bruno Santos - wikignpi-preview2Left
// @author      Bruno Leonardo Michels - jQuery injector
// @description Move preview in online Wiki Editor for project members at Google Code Project Hosting.
// @include http://code.google.com/p/*/w/edit.do
// ==/UserScript==

function moover(){
  var tryagain=true;
  if(unsafeWindow.jQuery !== undefined){
    if((unsafeWindow.jQuery('#wikipreview').length>0) &&
       (unsafeWindow.jQuery('#wikipreviewtitle').length>0) &&
       (unsafeWindow.jQuery('form[name=wiki_edit]').length>0))
    {
      var wikipreview = unsafeWindow.jQuery('#wikipreview');
      var wikipreviewtitle = unsafeWindow.jQuery('#wikipreviewtitle');
      var formwiki_edit = unsafeWindow.jQuery('form[name=wiki_edit]');
      var wikieditor_tds = formwiki_edit.find('td.vt');

      //0 is the editor, 1 is the help bubble
      if(wikieditor_tds.length>1)
      {
        var helpBubble = unsafeWindow.jQuery(wikieditor_tds[1]);
        var mainTr = unsafeWindow.jQuery(helpBubble[0].parentNode);
        var table4Editor = unsafeWindow.jQuery(helpBubble[0].parentNode.parentNode);

        var newTd = unsafeWindow.jQuery('<td>').addClass('vt');
        mainTr.prepend(newTd);
        newTd.append(wikipreviewtitle,wikipreview);

        var newTr = unsafeWindow.jQuery('<tr>');
        table4Editor.append(newTr);
        newTr.append(helpBubble);

        tryagain=false;
      }
    }
  }
  if(tryagain) unsafeWindow.setTimeout(moover,500);
};

function LoadScript(file)
{
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = file;
  document.getElementsByTagName('head')[0].appendChild(script);
    Wait();
};

var is$Reg = unsafeWindow.$;
function Wait()
{
    if (unsafeWindow.jQuery !== undefined)
    {
        if (is$Reg !== undefined)
        {
            unsafeWindow.jQuery.noConflict();
        }
        unsafeWindow.setTimeout(moover,500);
    }
    else
    {
        setTimeout(Wait, 100);
    }
}

if (unsafeWindow.jQuery == undefined)
  LoadScript("http://code.jquery.com/jquery-latest.min.js");

